package com.uchengwang.ui.cm_ui.uu.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.uu.adapter.ClubInviteAdapter;
import com.uchengwang.ui.cm_ui.uu.bean.ClubInviteBean;
import com.uchengwang.ui.cm_ui.uu.http.UUHttpManager;

import java.util.List;

/**
 * Created by U城网 on 2017/8/11.
 * 社团邀请
 */

public class ClubInviteActivity extends BaseWithTitleActivity{
    private PtrClassicFrameLayout layout;
    private RecyclerAdapterWithHF mAdapter;
    private RecyclerView invite_rv;
    private ClubInviteAdapter adapter;

    private int page = 1, totalPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_group_invite);
        setTitle("社团通知");

        initView();
    }

    private void initView(){
        layout = (PtrClassicFrameLayout) findViewById(R.id.invite_layout);
        invite_rv = (RecyclerView) findViewById(R.id.invite_rv);

        initData();
    }

    private void initData(){
        adapter = new ClubInviteAdapter(this);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        invite_rv.setLayoutManager(new LinearLayoutManager(this));
        invite_rv.setAdapter(mAdapter);

        layout.setAutoLoadMoreEnable(true);
        layout.setLoadMoreEnable(true);

        layout.postDelayed(new Runnable() {

            @Override
            public void run() {
                layout.autoRefresh(true);
            }
        }, 150);

        layout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                layout.loadMoreComplete(true);
                getHttps(true);
            }
        });

        layout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totalPage){
                    //当前请求页大于总页数，不再请求
                    getHttps(false);
                } else {
                    layout.loadMoreComplete(false);
                }
            }
        });
    }

    /**
     * 获取社团邀请
     * */
    private void getHttps(final boolean isRefresh){
        switch (UChengApplication.userRole){
            case BaseConfig.STUDENT_ROLE:
                //获取社团邀请
                UUHttpManager.getManager().getClubInvites(this, page, 10, new UUHttpManager.ClubInviteListener() {
                    @Override
                    public void onResult(int stutas, int totalPages, List<ClubInviteBean> listBean) {
                        totalPage = totalPages;
                        if (isRefresh){
                            layout.refreshComplete();
                        }else {
                            layout.loadMoreComplete(true);
                        }

                        if (stutas ==0 && listBean != null){

                            if (isRefresh){
                                adapter.setData(listBean);
                            }else {
                                adapter.getData().addAll(listBean);
                                adapter.notifyDataSetChanged();
                            }
                        }

                    }
                });
                break;
            case BaseConfig.CLUB_ROLE:
                //获取社团邀请
                UUHttpManager.getManager().getInvites(this, page, 10, new UUHttpManager.ClubInviteListener() {
                    @Override
                    public void onResult(int stutas, int totalPages, List<ClubInviteBean> listBean) {
                        totalPage = totalPages;
                        if (isRefresh){
                            layout.refreshComplete();
                        }else {
                            layout.loadMoreComplete(true);
                        }

                        if (stutas ==0 && listBean != null){

                            if (isRefresh){
                                adapter.setData(listBean);
                            }else {
                                adapter.getData().addAll(listBean);
                                adapter.notifyDataSetChanged();
                            }
                        }

                    }
                });
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        List<ClubInviteBean> l = adapter.getData();
        l.clear();
        l = null;
    }
}
